package com.xc.demo.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.demo.entity.Employee;
import com.xc.demo.entity.HomeCategory;
import com.xc.demo.service.HomeCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/homeCategory")
public class HomeCategoryController {

    @Autowired
    public HomeCategoryService homeCategoryService;


    @PostMapping
    public Boolean saveCat(@RequestBody HomeCategory homeCategory){
        return homeCategoryService.saveOrUpdate(homeCategory);
    }


    //    更新用户数据
    @PutMapping
    public HomeCategory update(@RequestBody HomeCategory homeCategory){



        homeCategoryService.updateById(homeCategory);

        return homeCategory;


    }



    @GetMapping("/page")
    public IPage<HomeCategory> findPage(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                                    @RequestParam(defaultValue = "") String uname

    ){


        IPage<HomeCategory> page= new Page<>(pageNum,pageSize);

        QueryWrapper<HomeCategory> queryWrapper = new QueryWrapper<>();

        if(!"".equals(uname)){
            queryWrapper.like("category_name",uname);
        }


        queryWrapper.orderByAsc("id");


        return homeCategoryService.page(page,queryWrapper);
    }

}
